<template>
  <div>
    <el-form ref="deptForm" label-width="120px" :rules="rules" :model="form">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in mangerList"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          /> </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, updateDepartments } from '@/api/departments'
export default {
  name: '',
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    pid: {
      type: String,
      default: ''
    },
    isEdit: {
      type: Boolean
    },
    row: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      son: '我是son组件',
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      },
      mangerList: [],
      /* 校验 */
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门管理者', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {
    isEdit: {
      handler(newVal) {
        if (newVal) {
          //  表示isEdit：true  表示是点击的编辑进入的
          this.form = { ...this.row }
        }
      },
      immediate: true // 初次进入立即执行监听
    }
  },
  created() {
    // console.log(this, 8899)
    this.loadEmp()
  },
  methods: {
    hSubmit() {
      this.$refs['deptForm'].validate(async(valid) => {
        if (valid) {
          // alert('submit!')
          //  5-2 兜底校验满足成功后执行
          this.form.pid = this.pid
          //   const res = await addDepartments(this.form)
          this.isEdit ? this.doEdit() : this.doAdd()
          // console.log(res, 66)
          // 5-2-3 添加部门成功后
        //   if (res.code === 10000) {
          // 关闭对话框+清空表单数据+刷新页面
          // this.$emit('update:dialogFormVisible', false)
          // this.form = {
          //   name: '', // 部门名称
          //   code: '', // 部门编码
          //   manager: '', // 部门管理者
          //   introduce: '', // 部门介绍
          //   pid: ''
          // }
          // // 刷新页面
          // this.$emit('success')
          // this.$emit('success')
          // this.reset()
        //   }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async doAdd() {
      const res = await addDepartments(this.form)
      if (res.code === 10000) {
        this.$emit('success')
      }
    },
    async doEdit() {
      const res = await updateDepartments(this.form)
      if (res.code === 10000) {
        this.$emit('success')
      }
    },
    hCancel() {
      this.$emit('success')
    },
    async loadEmp() {
      const res = await getEmployeeSimple()
      // console.log(res, 5)
      this.mangerList = res.data
    },

    reset() {
      this.$refs.deptForm.resetFields()
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
